package com.xiaoyezi.demo2.Controller;

import java.util.ArrayList;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.xiaoyezi.demo2.DataSource.User;

@RestController
@CrossOrigin(value = "http://192.168.23.104:8082",allowCredentials = "true")
public class Controller {
    // 创建一个User类型的集合
    ArrayList<User> userList = new ArrayList<>();
    // 导入user实体类
    User user = new User();
    // POST请求
    @PostMapping("/login")
    // 创建用户
    public String login(String zhanghao, String password){
        
        // 查看数据库是否有该账户
        if (userList.size() == 0){
            return "未查询到用户！";
        }else{
            if (zhanghao == null){
                return "您输入的账号或密码有问题！";
            }else{
                if(user.getZhanghao().equals(zhanghao)){
                    if (user.getPassword().equals(password)) {
                        return "登录成功！";
                    }
                    else{
                        return "您输入的密码有问题！";
                    }
                }else{
                    return "您输入的账号有问题！";
                }
            }
        }
    }
    
    // POST请求
    @PostMapping(value = "createUser")
    // 创建用户
    public String createUser(@RequestBody String zhanghao,String password,String category){

        // 判断值是否为空
        // if (zhanghao == null  || password == null || category == null){
        //     return "不能为空！";
        // }
       
        // 添加用户
        if (user.getZhanghao() == null){
                user.setZhanghao(zhanghao);
                user.setPassword(password);
                user.setCategory(category);
                // userList.add(user);
                return "创建成功！";
        }else {
            if (user.getZhanghao() == zhanghao ){
                return "账号已存在！";

            }else {
                user.setZhanghao(zhanghao);
                user.setPassword(password);
                user.setCategory(category);
                // userList.add(user);
                return "创建成功！";
            }
        }
        
    }


}
   
